﻿using DLUProject.Domain;
using DLUProject.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ColorLife.Core.Helper;
using System.Text.RegularExpressions;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using StructureMap;
public partial class QuanLyKTX_Phong_InDanhSach : Page
{
    
    public PhongSinhVienModel Model { get; set; }
    public PhongExtension _phongServiceExt { get; set; }

    public QuanLyKTX_Phong_InDanhSach()
    {
        IContainer container = StructmapInitialize.Initialize();
        container.BuildUp(this);
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int IDPhong
    {
        get
        {
            if (Request.QueryString["id"] != null)
                return Server.UrlEncode(Request.QueryString["id"]).ToInt();
            return -1;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (!IsPostBack)
        {
            Model = _phongServiceExt.GetThongTinPhong(IDPhong);
            if (Model == null)
            {
                Response.Redirect("Default.aspx");
            }

            rptItems.DataSource = Model.SinhViens;
            rptItems.DataBind();
        }
    }

   // const string msgFormat = "table[{0}], tr[{1}], td[{2}], a: {3}, b: {4}";

    const string table_pattern = "<div.*?>(.*?)</div>";

   // const string tr_pattern = "<tr.*?>(.*?)</tr>";

   // const string td_pattern = "<td.*?>(.*?)</td>";

   // const string a_pattern = "<a href=\"(.*?)\"></a>";

  //  const string b_pattern = "<b>(.*?)</b>";
    private static List<string> GetContents(string input, string pattern)
    {
        MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline);
        List<string> contents = new List<string>();
        foreach (Match match in matches)
            contents.Add(match.Value);
        return contents;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();

        wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
        wc.Headers.Add("Accept", "*/*");
        wc.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
        wc.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

        string url = Request.Url.AbsoluteUri;
        string fileContent = wc.DownloadString(url);
        List<string> tableContents = GetContents(fileContent, table_pattern);
        string HTMLString = String.Join(" ", tableContents.ToArray());

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();

        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDoc.Add((IElement)htmlarraylist[k]);
        }

        pdfDoc.Close();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" + "filename=sample.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();

       
    }
}